﻿using UnityEngine;
using System.Collections;
using Assets.Scripts.Interfaces;
using System.Collections.Generic;

public class ObjectsPool: MonoBehaviour, IObjectPool
{
    private static ObjectsPool _instance;
    public static ObjectsPool instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = GameObject.FindObjectOfType<ObjectsPool>();
                DontDestroyOnLoad(_instance.gameObject);
            }
            return _instance;
        }
    }

    void Awake()
    {
        if (_instance == null)
        {
            _instance = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            if (this != _instance)
                Destroy(this.gameObject);
        }
    }

    public List<GameObject> Create(Transform parent, GameObject obj, int count)
    {
        List<GameObject> lst = new List<GameObject>();
        for (int i = 0; i < count; i++)
        {
            GameObject newObj = Instantiate(obj) as GameObject;
            newObj.transform.parent = parent;
            newObj.SetActive(false);
            lst.Add(newObj);
        }
        return lst;
    }

    public void Spawn(GameObject obj, Vector3 position, Quaternion rotation)
    {

        obj.transform.position = position;
        obj.transform.rotation = rotation;
        obj.SetActive(true);
    }

    public void Remove(GameObject obj)
    {
        obj.SetActive(false);
    }
}
